package com.yjd.back.contentmanager;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.base.service.IIntroDetailService;
import com.yjd.comm.doctor.service.IDoctorService;
import com.yjd.comm.enums.ReferenceTypeEnum;
import com.yjd.comm.fs.util.FileUtil;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.project.model.ProjectCaseModel;
import com.yjd.comm.project.model.ProjectModel;
import com.yjd.comm.project.service.IProjectCaseService;
import com.yjd.comm.project.service.IProjectService;
import com.yjd.comm.resource.model.ResourceModel;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台案例管理
 * Created by yangchangyan on 2017/7/13.
 */
@Controller("com.yjd.back.contentmanager.CaseControl")
@Scope("prototype")
@RequestMapping("/case")
public class CaseControl extends BaseControl {

    @Resource
    private IProjectCaseService projectCaseService;

    @Resource
    private IProjectService projectService;

    @Resource
    private IDoctorService doctorService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IIntroDetailService introDetailService;

    /**
     * 查询所有案例，分页查询
     * @return void
     * @throws
     */
    @RequestMapping(value = "/all")
    public String allCase() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "create_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.projectCaseService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/case/all"));

        ProjectCaseModel pcm = null;
        for(Object obj : pager.getPagerModel().getDataList()){
            pcm = (ProjectCaseModel) obj;
            pcm.setCase_pic(ServletUtil.getFileUrl(pcm.getCase_pic()));
            pcm.getStrMap().put("create_time", DateUtil.timestampToDateString(pcm.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            pcm.getStrMap().put("last_update_time", DateUtil.timestampToDateString(pcm.getLast_update_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);

        return "case.list";
    }

    /**
     * 发布新案例编辑页面
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add")
    public String addCasePage() throws Exception {
        request.setAttribute("ref_ids", doctorService.queryDoctorName(0L));
        return "case.add";
    }

    /**
     * 编辑页面
     * @return void
     * @throws
     */
    @RequestMapping(value = "/edit")
    public String editCasePage(Long id) throws Exception {
        ProjectCaseModel pcm = projectCaseService.getModelById(id, DbWREnums.READ);
        IntroDetailModel idm = introDetailService.getModelById(pcm.getDetail_id(), DbWREnums.READ);
        if(null != idm){
            pcm.getStrMap().put("detail", idm.getContent());
        }
        request.setAttribute("projectcase", pcm);
        request.setAttribute("ref_ids", doctorService.queryDoctorName(0L));
        return "case.add";
    }

    /**
     * 删除案例
     * @return void
     * @throws
     */
    @RequestMapping(value = "/delete")
    public void deleteCase(Long id) throws Exception {
        projectCaseService.delete(id);
        this.successMsg("删除案例成功");
    }

    /**
     * 发布新案例
     * @return void
     * @throws
     */
    @RequestMapping(value = "/add/submit")
    public void addCase(MultipartHttpServletRequest multipartRequest, ProjectCaseModel pcm) throws Exception {
        if(StringUtil.isEmpty(pcm.getCase_name())){
            FrameUtil.throwBaseException1("医生名不能为空");
        }

        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);

        // 上传案例图片
        ResourceModel res = new ResourceModel();
        res.setRef_type(ReferenceTypeEnum.PROJECTCASE.getCode());
        res.setRef_id(pcm.getCase_id());
        String image_path = FileUtil.uploadFileToRemote(multipartRequest.getFileMap(), ReferenceTypeEnum.PROJECTCASE, res);
        if(StringUtil.isNotEmpty(image_path)){
            pcm.setCase_pic(image_path);
        }

        projectCaseService.createOrUpdate(pcm, (String)paramMap.get("detail"));

        this.successMsg(ServletUtil.getUrl("/case/all"), "保存案例成功");
    }

    /**
     * 获取关联信息
     * @return void
     * @throws
     */
    @RequestMapping(value = "/relate/content")
    public void relateContent(int ref_type) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("is_used", "1");
        Map<Long, String> idAndName = new HashMap<Long, String>();
        if(1 == ref_type){
            List<OrganizationModel> orgs = organizationService.getModelList(paramMap, DbWREnums.READ);
            for(OrganizationModel org:orgs){
                idAndName.put(org.getOrg_id(), org.getOrg_name());
            }
        }

        if(2 == ref_type){
            List<ProjectModel> projects = projectService.getModelList(paramMap, DbWREnums.READ);
            for(ProjectModel pro:projects){
                idAndName.put(pro.getProject_id(), pro.getProject_name());
            }
        }

        if(3 == ref_type){
            idAndName.putAll(doctorService.queryDoctorName(0L));
        }

        paramMap.put("idAndName", idAndName);
        this.successData(paramMap);
    }
}
